// Created by Xslt generator for Eclipse.
// XSL :  not found (java.io.FileNotFoundException:  (The system cannot find the path specified))
// Default XSL used : easystruts.jar$org.easystruts.xslgen.JavaClass.xsl

package com.ba360.struts.action;

import java.sql.Connection;
import java.sql.Statement;
import java.util.Properties;

import javax.naming.Context;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.ba360.portal.common.PortalConstants;
import com.ba360.portal.database.DBConnectionImpl;
import com.ba360.portal.util.EmailSender;
import com.ba360.portal.util.Log;
import com.ba360.struts.form.ForgotPassword;

/**
 * ForgotPasswordAction.java created by EasyStruts - XsltGen.
 * http://easystruts.sf.net created on 05-08-2004
 * 
 * XDoclet definition:
 * 
 * @struts:action path="/portal/ResetPassword" name="Form" input="/form/.jsp"
 *                validate="true"
 * @struts:action-forward name="/portal/login.do" path="/portal/login.do"
 * @struts:action-forward name="/jsp/NotDataFound.jsp"
 *                        path="/jsp/NotDataFound.jsp"
 */
public class ForgotPasswordAction_23Feb09 extends Action {

	// --------------------------------------------------------- Instance
	// Variables

	// --------------------------------------------------------- Methods

	/**
	 * Method execute
	 * 
	 * @param ActionMapping
	 *            mapping
	 * @param ActionForm
	 *            form
	 * @param HttpServletRequest
	 *            request
	 * @param HttpServletResponse
	 *            response
	 * @return ActionForward
	 * @throws Exception
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ForgotPassword ForgotPassword = (ForgotPassword) form;
		String forward = "failure";
		//throw new UnsupportedOperationException("Generated method
		// 'execute(...)' not implemented.");
		Log.init(this.getClass());
		Context context = null;
		DataSource ds = null;
		Connection conn = null;
		Statement stmt = null;
		/*
		 * context = new InitialContext(); ds =
		 * (DataSource)context.lookup("java:comp/env/jdbc/portalDB");
		 */
		DBConnectionImpl dbConn = new DBConnectionImpl();
		conn = dbConn.getConnection();
		//conn = ds.getConnection();
		stmt = conn.createStatement();
		String action = request.getParameter(PortalConstants.ACTION);
		if (action != null) {

			if (action.equals("Reset")) {
				String userId = dLookUp(stmt, "userinfo", "userid",
						"emailaddress='" + ForgotPassword.getEmailAddress()
								+ "'");
				Log.info("For Forgot Passs :" + userId + " email:"
						+ ForgotPassword.getEmailAddress());
				if (userId != null && userId.length() > 0) {
					forward = "success";
					request.setAttribute("FORGOTPASS", "FORGOTPASS");
					Properties prop = new Properties();
					String fn = dLookUp(stmt, "userinfo", "FirstName",
							"UserId=" + Integer.parseInt(userId)
									);
					String ln = dLookUp(stmt, "userinfo", "LastName",
							"UserId=" + Integer.parseInt(userId));
					
					String pwd = dLookUp(stmt, "userauth", "password",
							"UserId=" + Integer.parseInt(userId));
					
					prop.setProperty("FullName",fn+" "+ln);
					prop.setProperty("Password", pwd);
					prop.setProperty("ToEmailAddr",ForgotPassword.getEmailAddress());
					try{
						EmailSender.sendEmail(EmailSender.TYPE_RESETPASS,prop);
					}catch (Exception e)
					{
						Log.error(e.getMessage());
					}
					
					//send email
				} else {
					request.setAttribute("NOUSER", "USERNOTFOUND");
				}

			}
		}
		conn.close();
		return mapping.findForward(forward);
	}

	java.sql.ResultSet openrs(java.sql.Statement stat, String sql)
			throws java.sql.SQLException {
		java.sql.ResultSet rs = stat.executeQuery(sql);
		return (rs);
	}

	private String dLookUp(java.sql.Statement stat, String table, String fName,
			String where) {
		String res = null;
		Log.info("UserId:" + res + "  " + "SELECT " + fName + " FROM " + table
				+ " WHERE " + where);
		try {
			java.sql.ResultSet rs1 = openrs(stat, "SELECT " + fName + " FROM "
					+ table + " WHERE " + where);
			rs1.next();
			res = rs1.getString(1);
			Log.info("UserId:" + res + "  " + "SELECT " + fName + " FROM "
					+ table + " WHERE " + where);
			rs1.close();
	
		} catch (Exception e) {
			Log.error(e.getMessage());
		}
		return (res);
	}

}